package com.ljkj.cordial.chat.presenter.response;

import android.app.Activity;
import android.text.TextUtils;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.extra.UserChatTable;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.utils.CollectionUtils;
import com.lanjiu.pro.business.BusinessProtocolMessageStandard;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.MsgType;
import com.ljkj.cordial.chat.dao.ChatTableBean;
import com.ljkj.cordial.chat.dao.notify.ChatContentTable;
import com.ljkj.cordial.chat.dao.notify.FriendTable;
import com.ljkj.cordial.chat.dao.notify.GroupMemberTable;
import com.ljkj.cordial.chat.util.ReadFinishCycTimeUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * @description:
 * @author: Panwei
 * @date: 2020/3/21 15:31
 */
public class MessageClearProtocolResponse extends AResponseBusinessPresenter {

    @Override
    public void onResponse(Activity activity, boolean isOnScreen, String msgType, Object msg) {
        if (msgType.equals(IMSContacts.MsgType.MESSAGE_CLEAR) && msg instanceof BusinessProtocolMessageStandard.Head) {
            BusinessProtocolMessageStandard.Head response = (BusinessProtocolMessageStandard.Head) msg;

            String id = response.getFromId();
            long timeMillis = response.getTimestamp();

            // 删除相关消息
            String ids = ChatTableBean.regularClearance(id, timeMillis);

            // 更新好友的ChatContentTable中的Last，直接清空
            FriendTable friend = FriendTable.getTableByFriendUserId(id);
            if (friend != null) {
                boolean isReadFinish = ReadFinishCycTimeUtil.getInstance().isReadFinish(activity, Canstants.isSingleReadFinishKey(id, ChatApp.getInstance().getUserId()));
                List<ChatTableBean> chatTableBeans = ChatTableBean.queryByFriendId(friend.getFriendUserId());
                if (!CollectionUtils.isEmpty(chatTableBeans)) {
                    ChatTableBean last = chatTableBeans.get(chatTableBeans.size() - 1);
                    ChatContentTable.updateLastMsgByIdNotInsert(id, friend.getFriendNickname(), isReadFinish, last.getChatType(), last.getTextMsg(), last.getHistoryTime(), Canstants.SINGLE_CHAT, false, UserChatTable.RECEIVE_FROM_ACTIVITY, friend.getFriendAlias());
                } else {
                    ChatContentTable.updateLastMsgByIdNotInsert(id, friend.getFriendNickname(), isReadFinish, MsgType.Text, null, -1, Canstants.SINGLE_CHAT, false, UserChatTable.RECEIVE_FROM_ACTIVITY, friend.getFriendAlias());
                }
            }

            // 更新群组的ChatContentTable中的Last
            List<GroupMemberTable> groupMemberTables = GroupMemberTable.queryMembers(id);
            if (!CollectionUtils.isEmpty(groupMemberTables)) {
                for (GroupMemberTable groupMemberTable : groupMemberTables) {
                    boolean isReadFinish = ReadFinishCycTimeUtil.getInstance().isReadFinish(activity, Canstants.isGroupReadFinishKey(groupMemberTable.getGroupId(), ChatApp.getInstance().getUserId()));
                    List<ChatTableBean> chatTableBeans = ChatTableBean.queryByGroupId(groupMemberTable.getGroupId());
                    if (!CollectionUtils.isEmpty(chatTableBeans)) {
                        ChatTableBean last = chatTableBeans.get(chatTableBeans.size() - 1);
                        ChatContentTable.updateLastMsgByIdNotInsert(groupMemberTable.getGroupId(), groupMemberTable.getGroupName(), isReadFinish, last.getChatType(), last.getTextMsg(), last.getHistoryTime(), Canstants.GROUP_CHAT, false, UserChatTable.RECEIVE_FROM_ACTIVITY, null);
                    } else {
                        ChatContentTable.updateLastMsgByIdNotInsert(groupMemberTable.getGroupId(), groupMemberTable.getGroupName(), isReadFinish, MsgType.Text, null, -1, Canstants.GROUP_CHAT, false, UserChatTable.RECEIVE_FROM_ACTIVITY, null);
                    }
                }
            }

            if (!TextUtils.isEmpty(ids)) {
                MessageEvent event = MessageEvent.cerateUiRefresh(EventConstants.Action.MESSAGE_CLEAR);
                event.setMsg(id);
                event.setData(ids);
                EventBus.getDefault().post(event);
            }
        }
    }
}
